package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.ArrangeDiary;
import com.itheima.POJO.Member;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.ArrangeDiaryMapper;
import com.itheima.dao.memberMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.ArrangeDiaryService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * 随访日记service服务实现类
 */
@Service(interfaceClass = ArrangeDiaryService.class )
@Transactional
public class ArrangeDiaryServiceImpl implements ArrangeDiaryService {

    @Autowired
    private ArrangeDiaryMapper arrangeDiaryMapper;
    @Autowired
    private memberMapper memberMapper;


    /**
     * 分页查询随访日记列表数据(模糊查询)
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Map> page = arrangeDiaryMapper.pageQuery(queryString);
        long total = page.getTotal();
        List<Map> result = page.getResult();
        return new PageResult(total,result);
    }

    /**
     * 新增随访日记信息
     * @param formData
     * @param fileList
     * @return
     */
    @Override
    public Result addarrangeDiary(Map formData, Map fileList) throws Exception {

        String memberName = (String) formData.get("beIntervenePeople");
        String fileNumber = (String) formData.get("fileNumber");
        Member member = new Member();
        member.setName(memberName);
        member.setFileNumber(fileNumber);
        //判断所输入的回访名和档案号是否在会员库中
        member = memberMapper.findMemberByMemberNameAndFileNumber(member);
        if(member == null){
            //为空,所输入的会员不存在
            return new Result(false, MessageConstant.GET_ARRANGE_DIARY_MEMBER_FAIL);
        }
        //不为空,用户存在,新增随访日记
        ArrangeDiary arrangeDiary = new ArrangeDiary();
        arrangeDiary.setMemberId(member.getId()); //会员id
        arrangeDiary.setReturnDate(DateUtils.parseString2Date((String) formData.get("returnDate"))); //回访日期
        arrangeDiary.setMethod((String) formData.get("method"));//回访方式
        arrangeDiary.setVisitContext((String) formData.get("visitContext"));//回访内容
        arrangeDiary.setFeedback((String) formData.get("feedback"));//反馈信息
        arrangeDiary.setResultCode((String) formData.get("resultCode"));//处理结果
        arrangeDiary.setVisitPeople((String) formData.get("visitPeople"));//回访人员
        arrangeDiary.setTheme((String) formData.get("theme"));//主题
        arrangeDiary.setOriginalFilename((String) fileList.get("name"));//数据文件原名
        arrangeDiary.setFileName((String) fileList.get("fileName"));//数据文件存档名
        //新增随访日记
        arrangeDiaryMapper.addArrangeDiary(arrangeDiary);

        return new Result(true,MessageConstant.ADD_ARRANGE_DIARY_SUCCESS);
    }

    /**
     * 根据id删除随访日记信息
     * @param id
     */
    @Override
    public void deleteArrangeDiary(Integer id) {
        arrangeDiaryMapper.deleteArrangeDiary(id);
    }

    /**
     * 根据id查询随访日记信息
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findArrangeDiaryById(Integer id) {
        Map<String, Object> map = arrangeDiaryMapper.findArrangeDiaryById(id);
        //表单map
        Map<String,Object> formData = new HashMap<>();
        //a.id,a.visitPeople,a.returnDate,a.method,a.visitContext,a.feedback,a.resultCode,m.name beIntervenePeople
        formData.put("id",(Integer)map.get("id"));
        formData.put("visitPeople",(String)map.get("visitPeople"));//回访人员
        formData.put("returnDate",(Date)map.get("returnDate"));//回访日期
        formData.put("method",(String)map.get("method"));//回访方式
        formData.put("visitContext",(String)map.get("visitContext"));
        formData.put("feedback",(String)map.get("feedback"));
        formData.put("resultCode",(String)map.get("resultCode"));//处理结果
        formData.put("beIntervenePeople",(String)map.get("beIntervenePeople"));//被干预人
        formData.put("fileNumber",(String)map.get("fileNumber"));//档案号
        formData.put("theme",(String)map.get("theme"));
        //文件map
        Map<String, Object> fileMap = new HashMap<>();
        fileMap.put("name",map.get("originalFilename"));
        fileMap.put("url","http://q1jygupxd.bkt.clouddn.com/" + (String)map.get("fileName"));
        List<Map> fileList = new ArrayList<>();
        fileList.add(fileMap);
        Map<String,Object> result = new HashMap();
        result.put("formData",formData);
        result.put("fileList",fileList);
        return result;
    }

    /**
     * 修改随访日记信息
     * @param formData
     * @param fileList
     * @return
     */
    @Override
    public Result editArrangeDiary(Map formData, Map<String, Object> fileList) throws Exception {
        ArrangeDiary arrangeDiary = new ArrangeDiary();
        arrangeDiary.setOriginalFilename((String) fileList.get("name"));//数据文件原名
        arrangeDiary.setFileName((String) fileList.get("fileName"));//数据文件存档名
        arrangeDiary.setId((Integer) formData.get("id"));
        arrangeDiary.setReturnDate(DateUtils.parseString2Date((String) formData.get("returnDate"))); //回访日期
        arrangeDiary.setMethod((String) formData.get("method"));//回访方式
        arrangeDiary.setVisitContext((String) formData.get("visitContext"));//回访内容
        arrangeDiary.setFeedback((String) formData.get("feedback"));//反馈信息
        arrangeDiary.setResultCode((String) formData.get("resultCode"));//处理结果
        arrangeDiary.setVisitPeople((String) formData.get("visitPeople"));//回访人员
        arrangeDiary.setTheme((String) formData.get("theme"));//主题

        //修改随访日记信息
        arrangeDiaryMapper.editArrangeDiary(arrangeDiary);
        return new Result(true,MessageConstant.EDIT_ARRANGE_DIARY_SUCCESS);
    }
}
